package com.tianchi.lexiang.deep;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.VideoView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.bean.LeXiangConstants;
import com.tianchi.lexiang.service.VideoPlayerService;

public class VideoFullScreen extends Activity {
	private static final String TAG = "VideoFullScreen";
	private VideoView mVideo;
	private boolean isFinished;
	private Uri currentSongUri;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.video_full);
		mVideo = (VideoView) findViewById(R.id.video);
		isFinished = false;
	};

	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(TAG, "onRestart");
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Log.d(TAG, "onNewIntent");
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
		final int currentPosition = getIntent().getIntExtra(
				LeXiangConstants.CURRENT_POSITION, 0);
		currentSongUri = getIntent().getParcelableExtra(
				LeXiangConstants.CURRENT_SONGURI);
		Log.d(TAG, "currentPosition==>" + currentPosition
				+ ";;currentSongUri==>" + currentSongUri);
		mVideo.setVideoURI(currentSongUri);
		mVideo.seekTo(currentPosition);
		mVideo.start();
	}

	@Override
	protected void onResume() {
		super.onResume();
		isFinished = false;
	}

	@Override
	protected void onStop() {
		super.onStop();
		mVideo.stopPlayback();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_UP:
			Log.d(TAG, "onTouchEvent isFinished=>" + isFinished);
			if (!isFinished) {
				int currentPosition = mVideo.getCurrentPosition();
				// moveTaskToBack(false);
				finish();
				Intent startService = new Intent(this, VideoPlayerService.class);
				startService.putExtra(VideoPlayerService.COMMAND,
						VideoPlayerService.BACK_FROM_FULLSCREEN);
				startService.putExtra(LeXiangConstants.CURRENT_SONGURI,
						currentSongUri);
				startService.putExtra(LeXiangConstants.CURRENT_POSITION,
						currentPosition);
				Log.d(TAG, "will start videoplayerservice");
				startService(startService);
				isFinished = true;
			}
			break;
		default:
			break;
		}

		return true;
	}

}
